<template>
  <a-modal
    title="编辑用户问卷"
    :width="900"
    :visible="visible"
    :confirmLoading="confirmLoading"
    @ok="handleSubmit"
    @cancel="handleCancel"
  >
    <a-spin :spinning="confirmLoading">
      <a-form :form="form">
        <a-form-item v-show="false"><a-input v-decorator="['id']" /></a-form-item>
        <a-form-item label="审核" :labelCol="labelCol" :wrapperCol="wrapperCol" has-feedback>
          <a-input
            @change="setStatus"
            placeholder="请输入审核 2驳回 1通过"
            v-decorator="['receive', { rules: [{ required: true, message: '请输入审核！' }] }]"
          />
        </a-form-item>
        <a-form-item label="审核人" :labelCol="labelCol" :wrapperCol="wrapperCol" has-feedback>
          <a-input
            placeholder="请输入审核人"
            v-decorator="['name', { rules: [{ required: true, message: '请输入审核人！' }] }]"
          />
        </a-form-item>
        <a-form-item label="驳回意见" :labelCol="labelCol" :wrapperCol="wrapperCol" has-feedback v-show="re">
          <a-input
            placeholder="请输入驳回意见"
            v-decorator="['remarks', { rules: [{ message: '请输入驳回意见' }] }]"
          />
        </a-form-item>
      </a-form>
    </a-spin>
  </a-modal>
</template>

<script>
import moment from 'moment'
import { qusstionnaireEdit } from '@/api/modular/main/qusstionnaire/qusstionnaireManage'
import { mapGetters } from 'vuex'

export default {
  data() {
    return {
      labelCol: {
        xs: { span: 24 },
        sm: { span: 5 },
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 15 },
      },
      examineTimeDateString: '',
      visible: false,
      confirmLoading: false,
      re: false,
      form: this.$form.createForm(this),
      selectOptions: [
        { value: '1', label: '通过' },
        { value: '2', label: '驳回' },
      ],
      id: '',
    }
  },
  methods: {
    ...mapGetters(['nickname', 'avatar']),
    moment,
    setStatus(data) {
      this.re = false
      if (data.data == 2) {
        this.re = true
      } else {
        this.re = false
      }
    },
    // 初始化方法
    edit(record) {
      this.visible = true
      console.log(record)
      this.id = ''
      setTimeout(() => {
        this.form.setFieldsValue({
          id: record.id,
          receive: record.examine,
          name: nickname(),
          remarks: '',
        })
      }, 100)
      // 时间单独处理
      if (record.examineTime != null) {
        this.form.getFieldDecorator('examineTime', { initialValue: moment(record.examineTime, 'YYYY-MM-DD') })
      }
      this.id = record.id
      this.examineTimeDateString = moment(record.examineTime).format('YYYY-MM-DD')
    },
    handleSubmit() {
      const {
        form: { validateFields },
      } = this
      this.confirmLoading = true
      validateFields((errors, values) => {
        if (!errors) {
          for (const key in values) {
            if (typeof values[key] === 'object') {
              values[key] = JSON.stringify(values[key])
            }
          }
          values.id = this.id
          console.log(values)
          // values.examineTime = this.examineTimeDateString
          qusstionnaireEdit(values)
            .then((res) => {
              if (res.success) {
                this.$message.success('审核成功')
                this.confirmLoading = false
                this.$emit('ok', values)
                this.handleCancel()
                this.visible = false
              } else {
                this.$message.error('审核失败') //  + res.message
                this.visible = false
              }
            })
            .finally((res) => {
              this.confirmLoading = false
              this.visible = false
            })
        } else {
          this.confirmLoading = false
          this.visible = false
        }
      })
    },
    onChangeexamineTime(date, dateString) {
      this.examineTimeDateString = dateString
    },
    handleCancel() {
      this.form.resetFields()
      this.visible = false
    },
  },
}
</script>
